#include "../mqserver/mq_consumer.hpp"
#include <gtest/gtest.h>

wsmq::ConsumerManager::ptr cmp;

class consumerTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        cmp = std::make_shared<wsmq::ConsumerManager>();
        cmp->initQueueConsumer("queue1");
    }
    virtual void TearDown() override
    {
        cmp->clear();
    }
};

void cb(const std::string& tag, const wsmq::BasicProperties* bp, const std::string& body)
{
    std::cout << "%s消费了数据" << std::endl;
}

TEST(consumerTest, insert_test)
{
    auto cp1 = cmp->insert("consumer-1", "queue1", false, cb);
    auto cp2 = cmp->insert("consumer-2", "queue1", false, cb);
    auto cp3 = cmp->insert("consumer-3", "queue1", false, cb);

    ASSERT_NE(cp1.get(), nullptr);
    ASSERT_NE(cp2.get(), nullptr);
    ASSERT_NE(cp3.get(), nullptr);

    ASSERT_EQ(cmp->exist("consumer-1", "queue1"), true);
    ASSERT_EQ(cmp->exist("consumer-2", "queue1"), true);
    ASSERT_EQ(cmp->exist("consumer-3", "queue1"), true);
}

TEST(consumerTest, choose_test)
{
    auto cp1 = cmp->choose("queue1");
    ASSERT_EQ(cp1->_tag, "consumer-1");
    ASSERT_EQ(cp1->_qname, "queue1");

    auto cp2 = cmp->choose("queue1");
    ASSERT_EQ(cp2->_tag, "consumer-2");
    ASSERT_EQ(cp2->_qname, "queue1");

    auto cp3 = cmp->choose("queue1");
    ASSERT_EQ(cp3->_tag, "consumer-3");
    ASSERT_EQ(cp3->_qname, "queue1");
}

TEST(consumerTest, remove_test)
{
    cmp->remove("consumer-1", "queue1");
    cmp->remove("consumer-1", "queue2");
    cmp->remove("consumer-2", "queue1");

    auto cp = cmp->choose("queue1");
    ASSERT_EQ(cp->_tag, "consumer-3");
    ASSERT_EQ(cp->_qname, "queue1");
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new consumerTest);
    return RUN_ALL_TESTS();
}
